package com.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.common.R;
import com.cw.config.JwtConfig;
import com.cw.entity.Employee;
import com.cw.entity.dto.DishDto;
import com.cw.entity.Dish;
import com.cw.entity.DishFlavor;
import com.cw.mapper.DishMapper;
import com.cw.service.DishFlavorService;
import com.cw.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 添加菜品和口味
     * @param dishDto
     */
    @Override
    public void saveWithFlavors(DishDto dishDto, HttpServletRequest request) {
        //1.保存菜品基本信息
        Employee employee =(Employee) this.redisTemplate.opsForHash().get("EMP" , JwtConfig.getCurrentId(request));
        assert employee != null;
        dishDto.setWindowId(employee.getWindowId());
        this.save(dishDto);
        //2.保存口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //2.1解决dishID为空问题
        flavors =  flavors.stream().map(item->{
            //把集合中的每一项拿出来筛选
            item.setDishId(dishDto.getId());
            return  item;
        }).collect(Collectors.toList());//把每一项重新收集成一个新的集合
        //2.2再去执行批量添加
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 根据id查询，菜的基本信息和口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //1.查询菜品的基本信息
        Dish dish = this.getById(id);

        //2.查询这个菜的口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(wrapper);


        //3.dish+flavors = disdto
        DishDto dishDto = new DishDto();
        dishDto.setFlavors(dishFlavors);

        BeanUtils.copyProperties(dish, dishDto);


        return dishDto;
    }


    /**
     * 编辑的保存操作
     * @param dishDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateWithFlavor(DishDto dishDto) {
//        String windowId = this.baseMapper.selectById(dishDto.getId()).getWindowId();
//        Employee employee=(Employee)this.redisTemplate.opsForValue().get("EMP:"+BaseContext.getCurrentId());
//        if (!employee.getWindowId().equals(windowId)){
//            return R.error("非对应窗口员工，无法修改!");
//        }
        //1.修改基本数据类型
        this.updateById(dishDto);


        //2.修改口味集合,删除+新增=修改
        List<DishFlavor> flavors = dishDto.getFlavors();
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);

        //3.自己新增的数据没有id所以需要手动添加
        List<DishFlavor> list = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //批量添加
        dishFlavorService.saveBatch(list);
        return R.success("修改成功！");
    }

}
